package org.ala.tiktools.sharddb.mybatis.summary.impls;

import org.ala.tiktools.sharddb.mybatis.router.ShardInfo;
import org.ala.tiktools.sharddb.mybatis.summary.IShardListSummary;

import java.util.*;
import java.util.stream.Collectors;

/**
 * List<Integer>结构的返回值
 *
 * @author ala
 * @date 2025-02-25 13:14
 */
public class ShardListSumSummary implements IShardListSummary<Integer> {

    @Override
    public List<Integer> summary(Map<ShardInfo, List<Integer>> result) {
        if (result == null || result.isEmpty()) {return Arrays.asList();}

        int[] res = new int[result.entrySet().iterator().next().getValue().size()];
        result.forEach((k,v) -> {
            for (int i = 0; i < v.size(); i++) {
                res[i] += v.get(i);
            }
        });
        return Arrays.stream(res).boxed().collect(Collectors.toList());
    }
}
